package com.itheima.ydd.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.Result;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.service.SysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    //添加
    @Override
    public Result addSysMenu(Map<String, Object> map) {
        String jsonString = JSON.toJSONString(map);
        SysMenu sysMenu = JSON.parseObject(jsonString,SysMenu.class);
        boolean falg = this.save(sysMenu);
        return new Result(0,falg?"添加成功":"添加失败");
    }

    //根据id删除
    @Override
    public Result deleteById(Integer id) {

        //1.判断所删除的菜单底下是否还有别的菜单
        //利用pid去查看
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getPid,id);
        List<SysMenu> menus = this.list(wrapper);
        if (menus.size()>0){
            return new Result(302,"有子菜单不能删除");
        }
        boolean falg = this.removeById(id);

        return new Result(0,falg?"删除成功":"删除失败");
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<SysMenuDto> allData() {
     //1.拿到父级菜单的评判标准是pid=0&&type=1
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getPid,0).eq(SysMenu::getType,1);
        List<SysMenu> sysMenus = this.list(wrapper);
        List<SysMenuDto> sysMenuDtoList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            SysMenuDto sysMenuDto = new SysMenuDto();
            BeanUtils.copyProperties(sysMenu,sysMenuDto);
            //2.拿到次级菜单
            LambdaQueryWrapper<SysMenu> Childwrapper = new LambdaQueryWrapper<>();
            Childwrapper.eq(SysMenu::getPid,sysMenu.getId()).eq(SysMenu::getType,1);
            List<SysMenu> sysMenus1 = this.list(Childwrapper);
            List<SysMenuDto> childtoList = new ArrayList<>();
            for (SysMenu menu : sysMenus1) {
                SysMenuDto menuDto = new SysMenuDto();
                BeanUtils.copyProperties(menu,menuDto);
                //3.拿到孙级菜单
                LambdaQueryWrapper<SysMenu> pper = new LambdaQueryWrapper<>();
                pper.eq(SysMenu::getPid,menu.getId()).eq(SysMenu::getType,2);
                List<SysMenu> list = this.list(pper);
                List<SysMenuDto> sysMenuDtolist = new ArrayList<>();
                for (SysMenu sysMenu1 : list) {
                    SysMenuDto sysMenuDto1 = new SysMenuDto();
                    BeanUtils.copyProperties(sysMenu1,sysMenuDto1);
                    sysMenuDtolist.add(sysMenuDto1);
                }
                menuDto.setChildren(sysMenuDtolist);
                childtoList.add(menuDto);

            }
            sysMenuDto.setChildren(childtoList);
            sysMenuDtoList.add(sysMenuDto);
        }
        return sysMenuDtoList;
    }
}
